package cn.myframe.utils.core;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import cn.myframe.utils.xss.SQLFilter;

/**
 * 
 * 查询参数
 * 
 * @author  ynz
 * @email   ynz@myframe.cn
 * @version 创建时间：2018年6月8日 上午11:45:27
 */
public class Query extends LinkedHashMap<String, Object> {
	private static final long serialVersionUID = 1L;
	//当前页码
    private int page;
    //每页条数
    private int limit;
    
    public String hashCode;


	public Query(Map<String, Object> params){
        this.putAll(params);
        
        
        //分页参数
        if(params.get("page")!=null&&params.get("limit")!=null){
        	this.page = Integer.parseInt(params.get("page").toString());
            this.limit = Integer.parseInt(params.get("limit").toString());
            this.put("offset", (page - 1) * limit);
            this.put("page", page);
            this.put("limit", limit);
        }

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        String sidx = null;
        if(params.get("sidx")!=null){
        	sidx= params.get("sidx").toString();
        }
        String order = null;
        if( params.get("order") != null){
        	order = params.get("order").toString();
        }
        this.put("sidx", SQLFilter.sqlInject(sidx));
        this.put("order", SQLFilter.sqlInject(order));
        this.hashCode = BuildHashCode();
    	/*this.putAll(params);

        //分页参数
        this.page = Integer.parseInt(params.get("pageNumber").toString());
        this.limit = Integer.parseInt(params.get("pageSize").toString());
        this.put("offset", (page - 1) * limit);
        this.put("page", page);
        this.put("limit", limit);

        //防止SQL注入（因为sidx、order是通过拼接SQL实现排序的，会有SQL注入风险）
        String sidx = params.get("sortName").toString();
        String order = params.get("sortOrder").toString();
        this.put("sidx", SQLFilter.sqlInject(sidx));
        this.put("order", SQLFilter.sqlInject(order));*/
    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }
    
	public String getHashCode() {
		return hashCode;
	}

	public void setHashCode(String hashCode) {
		this.hashCode = hashCode;
	}


	public String BuildHashCode(){
    	StringBuffer sb = new StringBuffer();
    	Set<String> keySet = this.keySet();
    	if(keySet != null){
    		for(String key : keySet){
    			Object obj = this.get(key) ;
    			sb.append(String.valueOf(obj).hashCode());
    		}
    	}
    	sb.append(String.valueOf(this.page));
    	sb.append(String.valueOf(this.limit));
    	return sb.toString();
    }
}
